<template>
  <div class="page-container">
    <div class="page-header">
      <div class="page-tile">
        {{ $t('route.ruleIndex') }}
        <i v-if="false" class="iconfont iconzhongyiicon_gaojing"></i>
      </div>
      <div class="left-wrapper"></div>
      <div class="right-wrapper"></div>
    </div>
    <el-scrollbar class="page-content">
      <div class="content-wrapper">
        <el-card>
          <basic-table
            ref="ruleTable"
            :pageMethod="(params) => {
              params.search = searchValue;
              if (tableSearch) {
                      params.page = 1;
                      tableSearch = false;
                    }
              return $systemTunnyApi.getRulesList(params);
            }"
          >
            <template #table-options="tableSlotData">
              <div class="table-left-options">
                <el-button class="icon-button setting-button" style="width: auto;" :disabled="tableSlotData.table.selectRows.length <= 0" @click="() => {
                  $systemTunnyApi.changeRuleStatus(tableSlotData.table.selectRows, true).then(() => {
                    $message({
                      message: $t('desktopManage.message.operationSuccess', { operation: $t('user.Enable')}),
                      type: 'success'
                    });
                    tableSlotData.table.refresh();
                  }).catch((error) => {
                    $message({
                      message: error.msg,
                      type: 'warning'
                    });
                    tableSlotData.table.refresh();
                  });
                }">
                  <i class="el-icon-check" style="font-size: 12px; margin-right: 5px;"></i>
                  <span style="font-size: 12px;">{{ $t('user.Enable') }}</span>
                </el-button>
                <el-button class="icon-button setting-button" style="width: auto;" :disabled="tableSlotData.table.selectRows.length <= 0" @click="() => {
                  $systemTunnyApi.changeRuleStatus(tableSlotData.table.selectRows, false).then(() => {
                    $message({
                      message: $t('desktopManage.message.operationSuccess', { operation: $t('user.Disable')}),
                      type: 'success'
                    });
                    tableSlotData.table.refresh();
                  }).catch((error) => {
                    $message({
                      message: error.msg,
                      type: 'warning'
                    });
                    tableSlotData.table.refresh();
                  });
                }">
                  <i class="el-icon-close" style="font-size: 12px; margin-right: 5px;"></i>
                  <span style="font-size: 12px;">{{ $t('user.Disable') }}</span>
                </el-button>
              </div>
              <div class="table-right-options">
                <el-input :placeholder="$t('user.RolesManagerPageTableSearchPlaceholder')" @keyup.enter.native="tableSearchChange(tableSlotData)" v-model="searchValue" style="width: 200px;" >
                </el-input>
                <el-button @click="tableSearchChange(tableSlotData)" type="primary" style="margin-left: 10px;" >{{ $t('common.search') }}</el-button>
              </div>
            </template>
            <template #table-column="tableSlotData">
              <el-table-column type="selection" width="40" align="center"></el-table-column>
              <el-table-column
                prop="roleName"
                :label="$t('user.RoleName')"
              >
              </el-table-column>
              <el-table-column
                prop="isEnabled"
                :label="$t('common.status')"
              >
                <template #default="props">
                  <el-switch
                    :class="{
                      'el-switch-en': language === 'en',
                      'enabled-switch': true
                    }"
										v-model="props.row.isEnabled"
                    :disabled="!!props.row.isSystem"
										@change="changeDefault(props.row, props.row.isEnabled)"
                    :active-value="1"
                    :inactive-value="0"
                  >
									</el-switch>
                </template>
              </el-table-column>
              <el-table-column
                prop="createdAt"
                :label="$t('common.CreationTime')"
              >
                <template #default="props">
                  <span>{{ parseTime(new Date(props.row.createdAt)) }}</span>
                </template>
              </el-table-column>
              <el-table-column
                :label="$t('user.Action')"
                width="260"
              >
                <template #default="props">
                  <link-button :disabled="!!props.row.isSystem" :content="$t('common.edit')" :size="10" :font-size="'12px'" @click="$refs['RuleFormDialop'].init('edit', props.row)" style="margin-right: 15px;" />
                  <link-button :content="$t('user.detail')" :size="10" :font-size="'12px'" @click="showDetail(props.row)" style="margin-right: 15px;" />
                </template>
              </el-table-column>
            </template>
            <template #others-elements="tableSlotData">
              <rule-form-dialop ref="RuleFormDialop" @submit-success="tableSlotData.table.refresh()" />
            </template>
          </basic-table>
        </el-card>
      </div>
    </el-scrollbar>
  </div>
</template>
<script lang="ts">
import Vue from 'vue'
import BasicTable from '../../components/Basic/Table/BasicTable.vue';
import LinkButton from '../../components/Basic/Button/LinkButton.vue';
import RuleFormDialop from "./components/ruleFormDialop.vue";
import { AuthModule } from '../../store/modules/auth';
import { AppModule } from '../../store/modules/app';
import { parseTime } from '../../utils';
export default Vue.extend({
  components: {
    BasicTable,
    LinkButton,
    RuleFormDialop
  },
  computed: {
    AuthModule: () => AuthModule,
    console: () => console,
    parseTime: () => parseTime,
    language: () => AppModule.language
  },
  data() {
    return {
      searchValue: "",
      resetLoginFailCountLoading: false,
      tableSearch: false
    };
  },
  methods: {
    // 页面刷新重置page
    tableSearchChange(table: any) {
      (this as any).tableSearch = true;
      table.table.refresh(true);
    },
    deleteSystemManager(itemList: any) {
      for(let i=0;i<itemList.length;i++){
        if (itemList[i].id === AuthModule.userId) {
          (this as any).$message.warning((this as any).$t('user.deleteCurrentUser',{name: AuthModule.username}))
          return false;
        }
      }
      (this as any).$refs.DeleteDialog.init({
        type: 'system_manager',
        itemList
      });
    },
    resetPassword(row: any) {
      (this as any).$refs.ResetPasswordDialog.init({
        item: row,
        type: 'system_admin'
      });
    },
    changeDefault(row: any, isEnabled: number) {
      console.log("changeDefault", row, isEnabled);
      this.$systemTunnyApi.changeRuleStatus([row], !!isEnabled).then(() => {
        (this as any).$message({
          message: this.$t('desktopManage.message.operationSuccess', { operation: isEnabled ? this.$t('user.Enable') : this.$t('user.Disable') }),
          type: 'success'
        });
      }).finally(() => {
        (this as any).$refs.ruleTable.refresh();
      });
    },
    showDetail(row: any) {
      (this as any).$refs.RuleFormDialop.init("view", row);
    }
  }
})
</script>
<style lang="scss" scoped>
.page-container {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: #EFF1F4;

  .page-header {
    height: 60px;
    background-color: #FFFFFF;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #D9D9D9;

    .page-tile {
      color: #031129;
      font-size: 16px;
      margin-left: 20px;

      .iconzhongyiicon_gaojing {
        margin-left: 5px;
      }
    }
  }

  .page-content {
    height: calc(100vh - 110px);
    overflow: hidden;
    flex-grow: 1;

    .content-wrapper {
      padding: 15px;
    }
  }
}

.table-left-options {
  display: flex;
}

.table-right-options {
  display: flex;
 
  .setting-button {
    margin-right: 10px;
  }
}

.delete-warning-icon {
  color: #FF931D;
  font-size: 20px;
  margin-right: 10px;

  &::before {
    font-size: 20px;
  }
}

.enabled-switch {
  ::v-deep .el-switch {
    .el-switch__core::before {
      content: '禁用';
    }
    &.is-checked {
      .el-switch__core::before {
        content: '启用';
      }
    }
  }
}
</style>
